package com.my_musc_service.controller.api;

import com.my_musc_service.common.R;
import com.my_musc_service.entity.LoginInfoVO;
import com.my_musc_service.entity.LoginVO;
import com.my_musc_service.entity.User;
import com.my_musc_service.exception.CustomException;
import com.my_musc_service.interceptor.LoginInterceptor;
import com.my_musc_service.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Auther: yuansong
 * @Date: 2021/6/10 19:33
 * @Description: 登录相关
 */
@Slf4j
@RestController
@RequestMapping(value = "login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @GetMapping()
    public R login(LoginVO loginVO, HttpSession session) {
        Map<String, Object> map = loginService.login(loginVO, session);
        return R.ok().data(map);
    }

    @GetMapping("auth")
    public R getLoginInfo(HttpServletRequest request) {
        try {
            String jwtToken = request.getHeader("auth-token");
            LoginInfoVO loginInfoVO = loginService.getLoginInfoByJwtToken(jwtToken);
            return R.ok().data("userInfo", loginInfoVO);
        } catch (Exception e) {
            log.error("解析用户信息失败，" + e.getMessage());
            throw new CustomException("解析用户信息失败");
        }
    }

    @GetMapping("aa")
    public R getUser() {
        User userInfo = LoginInterceptor.getUserInfo();
        return R.ok().data("userInfo", userInfo);
    }


}
